package com.mflex.mwms.model;

import com.xuexiang.xhttp2.model.ApiResult;
import com.xuexiang.xutil.common.StringUtils;

/**
 * 自定义返回体
 * @param <T>
 */
public class CustomApiResult<T> extends ApiResult<T> {
    private int count;

    private String state;

    private String message;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "CustomApiResult{" +
                "count=" + count +
                ", state='" + state + '\'' +
                ", message='" + message + '\'' +
                '}';
    }

    @Override
    public int getCode() {

        int code;
        switch (state) {
            case "success":
                code = 0;
                break;
            case "error":
                if (StringUtils.equals("Token验证失败。", message)) {
                    code = 999;
                } else {
                    code = 100;
                }

                break;
            case "token":
                if (StringUtils.equals("Token验证失败。", message)) {
                    code = 999;
                } else {
                    code = 100;
                }

                break;
            case "waring":
                code = 200;
                break;
            default:
                code = 0;
                break;
        }
        return code;
    }

    @Override
    public String getMsg() {
        return message;
    }

    @Override
    public boolean isSuccess() {
        boolean flag;
        switch (state) {
            case "success":
                flag = true;
                break;
            case "error":
                flag = false;
                break;
            case "token":
                flag = false;
                break;
            case "waring":
                flag = false;
                break;
            default:
                flag = true;
                break;
        }
        return flag;
    }
}
